package com.steak.concurrent.Future.futuretask;

import com.steak.concurrent.common.R;

import java.util.concurrent.*;

/**
 * @author 刘牌
 * @Title:
 * @date 2022/3/269:28
 */
public class FutureTaskTest {
    static ExecutorService executorService = Executors.newCachedThreadPool();
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<R> task1 = task1();
        FutureTask<R> task2 = task2();
        executorService.submit(task1);
        executorService.submit(task2);
        System.out.println("start task 3");
        if (task1.get().getCode() == 200) System.out.println("task1 result is "+task1.get().getMsg());
        executorService.shutdownNow();
    }

    public static FutureTask<R> task1(){
        return new FutureTask<R>(new Callable<R>() {
            @Override
            public R call() throws Exception {
                System.out.println("start task 1");
                return new R(200,"task1",null);
            }
        });
    }

    public static FutureTask<R> task2(){
        return new FutureTask<R>(new Callable<R>() {
            @Override
            public R call() throws Exception {
                System.out.println("start task 2");
                return new R(200,"task2",null);
            }
        });
    }
}
